package cn.zl.springbootmqtt.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @Description:配置类-从yml配置中获取数据
 * @Author: zhanglang
 * @Date: 2020/10/27 14:24
 */
public class ymlUtil {
    public static String MQTT_HOST;
    public static String MQTT_CLIENTID;
    public static String MQTT_USER_NAME;
    public static String MQTT_PASSWORD;
    public static Integer MQTT_TIMEOUT;
    public static Integer MQTT_KEEP_ALIVE;

    static {
        MQTT_HOST = loadMqttYml().getProperty("host");
        MQTT_CLIENTID = loadMqttYml().getProperty("clientId");
        MQTT_USER_NAME = loadMqttYml().getProperty("username");
        MQTT_PASSWORD = loadMqttYml().getProperty("password");
        MQTT_TIMEOUT = Integer.valueOf(loadMqttYml().getProperty("timeout"));
        MQTT_KEEP_ALIVE = Integer.valueOf(loadMqttYml().getProperty("keepalive"));
    }

    /**
     * 从yml从获取数据
     * @return
     */
    private static Properties loadMqttYml() {
        InputStream inputstream = ymlUtil.class.getResourceAsStream("/application.yml");
        Properties properties = new Properties();
        try {
            properties.load(inputstream);
            return properties;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (inputstream != null) {
                    inputstream.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

}
